home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-17 | 12.2 KB | 455 lines | [TEXT/R*ch] |
- // DSeqChildApp.cpp
- // d.g.gilbert
-
-
- #include "DSeqChildApp.h"
-
- #include <ncbi.h>
- #include <dgg.h>
- #include <Dvibrant.h>
- #include <DControl.h>
- #include <DApplication.h>
- #include <DTask.h>
- #include <DMenu.h>
- #include <DPanel.h>
- #include <DUtil.h>
- #include <DWindow.h>
- #include <DChildApp.h>
-
- #include "DSeqFile.h"
- #include "DSeqList.h"
- #include "DSeqDoc.h"
-
-
- #if 0
- need 3 dialog methods here --
- * run app -- simple dlog w/ description, run button, help button, ?cancel button
- * edit child list -- list dlog w/ list of child apps, edit button, delete button, add button
- * edit child -- dlog w/ child app params (pathname, desc, cmdline,
- in/out files, min seqs, seqformat, other??)
-
- #endif
-
-
- class DSeqChildApp : public DChildApp
- {
- public:
- //char * fName, * fCmdline;
- //const char * fStdin, * fStdout, * fStderr;
- enum seqactions { kOpenSequence = 11, kOpenTree };
- char * fMenuname, * fDescription, * fSeqformat, *fHelpfile;
- short fMinseq, fMenucmd;
- DFile * fInfile;
-
- DSeqChildApp() :
- DChildApp(),
- fMenuname(NULL), fDescription(NULL), fSeqformat(NULL), fHelpfile(NULL),
- fInfile(NULL), fMinseq(0), fMenucmd(0)
- {
- fFiles= new DList();
- }
- virtual ~DSeqChildApp();
- virtual void LaunchDialog( DSeqList* aSeqList);
- virtual void FileAction( DChildFile* aFile);
- };
-
- class DLaunchDialog : public DWindow {
- enum { cHELP = 3222 };
- char *fHelpfile;
- DSeqChildApp* fChild;
- public:
- DLaunchDialog( DSeqChildApp* child);
- virtual void Open();
- virtual Nlm_Boolean IsMyAction(DTaskMaster* action);
- };
-
-
-
- DLaunchDialog::DLaunchDialog( DSeqChildApp* child) :
- DWindow( 0, gApplication, fixed, -10, -10, -50, -20, "Launch app"),
- fChild(child)
- {
- }
-
- void DLaunchDialog::Open()
- {
- char buf[128];
-
- if (fChild->fDescription) {
- DCluster* clu= new DCluster( 0, this, 0, 0, false, "Description"); //0,0 == wid,hi
- new DNotePanel(0, clu, fChild->fDescription, 200, 50);
- this->NextSubviewBelowLeft();
- }
- sprintf( buf, "Launch %s?",fChild->fMenuname);
- new DPrompt(0, this, buf, 0, 0, Nlm_systemFont);
- this->NextSubviewBelowLeft();
-
- this->AddOkayCancelButtons(cOKAY,"Launch",cCANC,"Cancel");
- if (fChild->fHelpfile) {
- fHelpfile= fChild->fHelpfile;
- this->NextSubviewToRight();
- new DButton( cHELP, this, "Help");
- }
- else
- fHelpfile= NULL;
-
- DWindow::Open();
- }
-
- Nlm_Boolean DLaunchDialog::IsMyAction(DTaskMaster* action)
- {
- switch(action->Id()) {
- case cHELP:
- gApplication->OpenDocument(fHelpfile);
- return true;
- default:
- return DWindow::IsMyAction(action);
- }
- }
-
-
-
-
-
- DSeqChildApp::~DSeqChildApp()
- {
- MemFree( fMenuname);
- MemFree( fDescription);
- MemFree( fSeqformat);
- MemFree( fHelpfile);
- }
-
- void DSeqChildApp::LaunchDialog( DSeqList* aSeqList)
- {
- DLaunchDialog* win= NULL;
- Boolean okay;
-
- if (fMinseq>0 && (!aSeqList || aSeqList->GetSize()<fMinseq)) {
- Message(MSG_OK, "%s needs a selection of at least %d sequences",
- fMenuname, fMinseq);
- return;
- }
-
- #ifdef WIN_MAC
- // This works for MAC but fails for MSWIN && UNIX!
- win= new DLaunchDialog(this);
- okay= win->PoseModally();
- delete win; // for MSWin, MUST delete win BEFORE LAUNCH !! -- now mswin fails here
- #else
- #if defined( NOT_WIN_MOTIF ) || defined( NOT_WIN_MSWIN)
- // bad problems in Unix/XWin w/ this dialog mangling child launch process...
- okay= true;
- #else
- // this works for MSWIN but fails on MAC !
- DLaunchDialog ldlog(this);
- okay= ldlog.PoseModally();
- #endif
- #endif
-
- if (okay) {
- // write aSeqList to this->inputfile...
- DFile* aFile = this->fInfile;
- if (aFile) {
- aFile->Open("w");
- short outformat= DSeqFile::FormatFromName(fSeqformat);
- if (outformat == DSeqFile::kUnknown)
- outformat= DSeqFile::kPearson; //?? or kPearson ?
- aSeqList->ClearSelections(); //! make sure we write all of seq from this call !?
- aSeqList->DoWrite( aFile, outformat);
- // ?? delete aSeqList if we own it !?
- //aFile->WriteData( buffer, buflen);
- aFile->Close();
- aFile->SetMode("r");
- }
-
- if (!this->Launch())
- Message(MSG_OK, "Failed to launch %s with '%s'", fName, fCmdline);
- }
- }
-
-
- void DSeqChildApp::FileAction( DChildFile* aFile)
- {
- switch (aFile->fAction) {
- case kOpenSequence:
- DSeqDoc::fgTestSeqFile= true; // ???
- gApplication->OpenDocument( aFile);
- DSeqDoc::fgTestSeqFile= false;
- break;
-
- case kOpenTree:
- // fix later...
- default:
- DChildApp::FileAction( aFile);
- break;
- }
- }
-
-
-
-
-
- // class DSeqApps : public DObject
-
-
- // static
- void DSeqApps::CallChildApp(short menucmd, DSeqList* aSeqList)
- {
- DSeqChildApp * child = NULL;
- short i, n = DChildAppManager::gChildList->GetSize();
- for (i= 0; i<n; i++) {
- child= (DSeqChildApp*) DChildAppManager::gChildList->At(i);
- if (child->fMenucmd == menucmd) {
- child->LaunchDialog( aSeqList);
- return;
- }
- }
- Message(MSG_OK, "Child app not found");
- }
-
-
-
- // static
- void DSeqApps::SetUpMenu(short menuId, DMenu*& aMenu)
- {
- char * kAppSection = "apps";
- long atsection;
- ulong sectlen = 0;
- char * value, * item, * appsection, * appvalues, * sections,
- * newpath, * cp;
- short menuitem= kChildMenuBaseID;
- DSeqChildApp * child;
- DChildFile * cfile;
- Boolean noStderr= true, noStdout= true, noStdin= true;
-
-
- //DWindow::SetUpMenu(menuId, aMenu);
- if (!aMenu) aMenu = gApplication->NewMenu( menuId, "ChildApps");
-
- sections= gApplication->GetPrefSection( kAppSection, sectlen);
- atsection= 0;
- while (atsection<sectlen) {
- appsection= sections + atsection;
- if (appsection) {
- long atvalue;
- ulong applen= 0;
-
- child= new DSeqChildApp();
- // child is stored in DChildAppManager::gChildList
-
- value= gApplication->GetPref( appsection, kAppSection);
- if (!value) value= StrDup( appsection);
- aMenu->AddItem( menuitem, value);
- child->fMenuname= value;
- child->fMenucmd= menuitem;
- menuitem++;
-
- child->fReusable= true; // don't delete after one use...
-
- appvalues= gApplication->GetPrefSection( appsection, applen);
- atvalue= 0;
- while (atvalue<applen) {
- item= appvalues + atvalue;
- value= gApplication->GetPref( item, appsection);
-
- if (!value)
- ;
- else if (StrICmp(item,"desc")==0)
- child->fDescription= value;
- else if (StrICmp(item,"path")==0) {
- cp= value; while (isspace(*cp) || *cp == '$') cp++;
- child->fName= gApplication->ConvertStdFilePath(cp);
- MemFree( value);
- }
- else if (StrICmp(item,"help")==0) {
- cp= value; while (isspace(*cp) || *cp == '$') cp++;
- child->fHelpfile= gApplication->ConvertStdFilePath(cp);
- MemFree( value);
- }
-
- else if (StrICmp(item,"seqformat")==0)
- child->fSeqformat= value;
- else if (StrICmp(item,"minseq")==0) {
- child->fMinseq= atoi(value);
- MemFree( value);
- }
-
- else if (StrICmp(item,"cmd")==0) {
- // convert variables of form $word[delimiter] ...
- // especially file path variables
- char * ep;
- char * newvalue = StrDup("");
- cp = value;
- do {
- ep= StrChr( cp, '$');
- if (ep) {
- *ep++ = 0;
- StrExtendCat( &newvalue, cp); // add on string before '$'
-
- // find & substitute any pathname
- newpath= gApplication->ConvertStdFilePath(ep);
- // newpath is all of remaining string ... cut pathname from remainder of str
- char* np= newpath;
- while (isgraph(*np)) np++; *np= 0;
- StrExtendCat( &newvalue, newpath);
- MemFree( newpath);
-
- // locate next part of str to parse -- cue on whitespace
- while (isgraph(*ep)) ep++;
- cp= ep;
- }
- else
- StrExtendCat( &newvalue, cp);
- } while (ep);
- MemFree( value);
- value= newvalue;
- child->fCmdline= value;
- }
-
- // need also to handle dialog control items !!, e.g., dlog=control spec...
- // and need to handle dlog control substitutions in cmdline...
-
- else if (StrNICmp(item,"infile",6)==0) {
- cp= value; while (isspace(*cp) || *cp == '$') cp++;
- newpath= gApplication->ConvertStdFilePath(cp);
- cfile= new DChildFile( newpath, DChildFile::kInput, DChildFile::kDeleteWhenDone,
- DChildFile::kNoAction, "r", "TEXT", "Spup");
- child->AddFile(cfile);
- child->fInfile= cfile; // ?? can we have more than one infile ??
- MemFree( newpath);
- MemFree( value);
- }
-
- else if (StrNICmp(item,"stdin",5)==0) {
- noStdin= false;
- cp= value; while (isspace(*cp) || *cp == '$') cp++;
- newpath= gApplication->ConvertStdFilePath(cp);
- cfile= new DChildFile( newpath, DChildFile::kStdin, DChildFile::kDontDelete,
- DChildFile::kNoAction, "r", "TEXT", "Spup");
- child->AddFile(cfile);
- MemFree( newpath);
- MemFree( value);
- }
-
- else if (StrNICmp(item,"stderr",6)==0) {
- noStderr= false;
- short action = DChildFile::kOpenText;
- char* ftype= StrChr(value,'\t'); // look for mime/type after name
- if (!ftype) ftype= StrChr(value,' '); // !? can we also scan for spaces ??
- if (ftype) {
- *ftype++= 0;
- while (*ftype && *ftype <= ' ') ftype++;
- if (StrNICmp(ftype,"biosequence",11) == 0
- || StrNICmp(ftype,"sequence",8) == 0)
- action= DSeqChildApp::kOpenSequence;
- else if (StrNICmp(ftype,"biotree",7) == 0
- || StrNICmp(ftype,"tree",4) == 0)
- action= DSeqChildApp::kOpenTree;
- }
- cp= value; while (isspace(*cp) || *cp == '$') cp++;
- newpath= gApplication->ConvertStdFilePath(cp);
- cfile= new DChildFile( newpath, DChildFile::kStderr, DChildFile::kDontDelete,
- action, "r", "TEXT", "Spup");
- child->AddFile(cfile);
- MemFree( newpath);
- MemFree( value);
- }
-
- else if (StrNICmp(item,"stdout",6)==0) {
- noStdout= false;
- short action = DChildFile::kOpenText;
- char* ftype= StrChr(value,'\t'); // look for mime/type after name
- if (!ftype) ftype= StrChr(value,' '); // !? can we also scan for spaces ??
- if (ftype) {
- *ftype++= 0;
- while (*ftype && *ftype <= ' ') ftype++;
- if (StrNICmp(ftype,"biosequence",11) == 0
- || StrNICmp(ftype,"sequence",8) == 0)
- action= DSeqChildApp::kOpenSequence;
- else if (StrNICmp(ftype,"biotree",7) == 0
- || StrNICmp(ftype,"tree",4) == 0)
- action= DSeqChildApp::kOpenTree;
- }
- cp= value; while (isspace(*cp) || *cp == '$') cp++;
- newpath= gApplication->ConvertStdFilePath(cp);
- cfile= new DChildFile( newpath, DChildFile::kStdout, DChildFile::kDontDelete,
- action, "r", "TEXT", "Spup");
- child->AddFile(cfile);
- MemFree( newpath);
- MemFree( value);
- }
-
- else if (StrNICmp(item,"outfile",7)==0) {
- // combine stdout & stderr w/ this code...
- short action = DChildFile::kOpenText;
- char* ftype= StrChr(value,'\t'); // look for mime/type after name
- if (!ftype) ftype= StrChr(value,' '); // !? can we also scan for spaces ??
- if (ftype) {
- *ftype++= 0;
- while (*ftype && *ftype <= ' ') ftype++;
- if (StrNICmp(ftype,"biosequence",11) == 0
- || StrNICmp(ftype,"sequence",8) == 0)
- action= DSeqChildApp::kOpenSequence;
- else if (StrNICmp(ftype,"biotree",7) == 0
- || StrNICmp(ftype,"tree",4) == 0)
- action= DSeqChildApp::kOpenTree;
- }
- cp= value; while (isspace(*cp) || *cp == '$') cp++;
- newpath= gApplication->ConvertStdFilePath(cp);
- cfile= new DChildFile( newpath, DChildFile::kOutput, DChildFile::kDontDelete,
- action, "r", "TEXT", "Spup");
- child->AddFile(cfile);
- MemFree( newpath);
- MemFree( value);
- }
-
-
- atvalue += StrLen(item)+1;
- }
-
- if (noStderr) child->AddFile(DChildFile::kStderr, NULL);
- if (noStdout) child->AddFile(DChildFile::kStdout, NULL);
-
- MemFree( appvalues);
- }
- atsection += StrLen(appsection)+1;
- }
- MemFree( sections);
- }
-
-
-
-
- #if 0
-
- /////// prefs file example ///////
- [apps]
- ;localvar=menu title
- clustal=Multiple align...
- cap=Contig assembly...
- dnaml=Phylip DNAML...
-
- [clustal]
- ;file path to application
- path=apps:clustalw
- help=apps:clustalw_help
- ;description for dialog
- desc=A multiple sequence alignment application
- ;dialog controls (HTML forms format?)
-
- ;command line switches
- cmd=/infile=temp:clustal.inseq /outfile=clustal.outseq /output=gcg /align
-
- ;sequence selection to input file
- infile=clustal.inseq
- ;sequence format to send to app
- seqformat=nbrf
- ;minimum number of selected sequences to activate this command
- minseq=2
- ;return data files, with doc/type
- outfile1=clustal.outseq text/plain
- outfile2=clustal.dnd text/plain
-
-
- #endif
-